package br.com.rglariguet.pioneer.dao;

import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Repository
@Transactional
public class DaoGenerico<T> {
	
	private SessionFactory factory;
	
	@Autowired
	public void setFactory(SessionFactory factory) {
		this.factory = factory;
	}

	public Session getSession() {
		return factory.getCurrentSession();
	}
	
	public T salvar(T modelo) {
		getSession().save(modelo);
		return modelo;
	}

	
	public void alterar(T modelo) {
		getSession().update(modelo);
	}

	public void merge(T modelo) {
		getSession().merge(modelo);
	}
	
	public void excluir(T modelo) {
		if (modelo != null) {
			getSession().delete(modelo);
		}
	}
	
}
